package com.groep14.project;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.groep14.model.Person;

/**
 * De NavigatieController handelt de "simpele" navigatie af tussen pagina's waar weining interactie met de database is.
 * 
 * @since r31 - 2 April 2012
 * @author Maarten Tutak
 *
 */

@Controller
public class NavigatieController {

	private static final Logger logger = LoggerFactory.getLogger(NavigatieController.class);
	
	/**
	 * De winkelwagen methode word gebruikt om naar de winkelwagenpagina te navigeren.
	 * @param session
	 * @return ModalAndView
	 */
	
	@RequestMapping(value = "/winkelwagen", method = RequestMethod.GET)
	public ModelAndView winkelwagen(HttpSession session) {
		 
		 Person p = (Person) session.getAttribute("person");
		 
		 //controleren of er een model is opgeslagen in een session
		 if (session.getAttribute("ShoppingCart") == null){
			 //nieuwe model aanmaken en laden
			ModelAndView modelAndView = new ModelAndView("winkelwagen");
			if(p != null) {
		        modelAndView.addObject("firstname", p.getFirstname());
	        }
				return modelAndView;
		 }else{
			 logger.info("model uit session halen");
			 //model uit session halen en laden
			 ModelAndView modelAndView = (ModelAndView) session.getAttribute("ShoppingCart");
			 if(p != null) {
			        modelAndView.addObject("firstname", p.getFirstname());
		        }
			 return modelAndView;
		 }

		 
	}
	
	/**
	 * De about methode word gebruikt om naar de aboutpagina te navigeren.
	 * @param session
	 * @return ModalAndView
	 */
	 
	@RequestMapping(value = "/about", method = RequestMethod.GET)
	public ModelAndView about(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView("about");
        Person p = (Person) session.getAttribute("person");
        
        if(p != null) {
	        modelAndView.addObject("firstname", p.getFirstname());
	        modelAndView.addObject("admin", p.getAdmin());
        }
        
		return modelAndView;
	}
		
/*	@RequestMapping(value = "/aanbieding", method = RequestMethod.GET)
	public ModelAndView aanbieding(HttpSession session) {
		
        ModelAndView modelAndView = new ModelAndView("aanbieding");
        Person p = (Person) session.getAttribute("person");
        
        if(p != null) {
	        modelAndView.addObject("firstname", p.getFirstname());
	        modelAndView.addObject("admin", p.getAdmin());
        }
        
		return modelAndView;		
	}*/
	
	/**
	 * De login methode word gebruikt om naar de loginpagina te navigeren.
	 * @param session
	 * @return ModalAndView
	 */
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)	
	public ModelAndView login(HttpSession session) {
		
		Person person = (Person) session.getAttribute("person");
		if(person != null){
			logger.info(person.getFirstname() +  " - You are already logged in ! ");
			return new ModelAndView("redirect:index");
		} else {
			return new ModelAndView("login");
		}
	}
	
	/**
	 * De registratie methode word gebruikt om naar de registratienpagina te navigeren.
	 * @param session
	 * @return ModalAndView
	 */
	
	@RequestMapping(value = "/registratie",method = RequestMethod.GET)
	public ModelAndView registratie(HttpSession session) {
		
        ModelAndView modelAndView = new ModelAndView("registratie");
        Person p = (Person) session.getAttribute("person");
        
        if(p != null) {
	        modelAndView.addObject("firstname", p.getFirstname());
	        modelAndView.addObject("admin", p.getAdmin());
	        return new ModelAndView("redirect:index");
        }
        
		return modelAndView;		
	}

	/**
	 * De inschrijven methode word gebruikt om naar de inschrijvingspagina te navigeren.
	 * @param session
	 * @return ModalAndView
	 */
	
	@RequestMapping(value = "/inschrijven",method = RequestMethod.GET)
	public ModelAndView inschrijven(HttpSession session) {
		
        ModelAndView modelAndView = new ModelAndView("inschrijven");
        Person p = (Person) session.getAttribute("person");
        
        if(p != null) {
	        modelAndView.addObject("firstname", p.getFirstname());
	        modelAndView.addObject("admin", p.getAdmin());
        }	        
        
		return modelAndView;		
	}

	/**
	 * De voltooid methode word gebruikt om naar de voltooidpagina te navigeren.
	 * @param session
	 * @return ModalAndView
	 */
	
	@RequestMapping(value = "/voltooid", method = RequestMethod.GET)
	public ModelAndView voltooid(HttpSession session) {
		
        ModelAndView modelAndView = new ModelAndView("voltooid");
        Person p = (Person) session.getAttribute("person");
        
        if(p != null) {
	        modelAndView.addObject("firstname", p.getFirstname());
	        modelAndView.addObject("admin", p.getAdmin());
        }
        
		return modelAndView;		
	}
	
}
